﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Delta
{
    public partial class FormLogin : Form
    {
        bool beginMove = false;//初始化鼠标位置
        int currentXPosition;
        int currentYPosition;

        public Form1 FormMain { get; set; }

        public FormLogin(Form1 f)
        {
            InitializeComponent();
            FormMain = f;
            User.IsLogin = false;
            GetLoginParam();
            txtName.Text = "工程师".ToString();           
        }
       
       
       

        #region 鼠标移动位置
        private void loginForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                beginMove = true;
                currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
                currentYPosition = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
            }
        }
        private void loginForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (beginMove)
            {
                this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
                this.Top += MousePosition.Y - currentYPosition;//根据鼠标的y坐标窗体的顶部，即Y坐标
                currentXPosition = MousePosition.X;
                currentYPosition = MousePosition.Y;
            }
        }
        private void loginForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentXPosition = 0; //设置初始状态
                currentYPosition = 0;
                beginMove = false;
            }
        }
        #endregion


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
             string str = Decrypt(User.Password, "1a2b3d4e");
            //string str = User.Password;
            if (txtName.Text=="工程师"&&txtPassword.Text==str)
            {
                User.IsLogin = true;
                
                this.Close();
                MessageBox.Show("登陆成功！");
                FormMain.AdminRight(User.IsLogin);
            }
            else
            {
                txtPassword.Clear();
                txtPassword.Focus();
                MessageBox.Show("密码与用户名不对应，请重新输入！");
            }
        }

        private void btnForgetPassWord_Click(object sender, EventArgs e)
        {
            FormModifyPSW modifyPSW = new FormModifyPSW();
            this.Close();
            modifyPSW.ShowDialog();
        }
        private void GetLoginParam()
        {
            FileInfo fileInfo = new FileInfo(Directory.GetCurrentDirectory() + "/configLogin.ini");
           
            if (!fileInfo.Exists)
            {
                MessageBox.Show("安装目录下配置文件configLogin.ini丢失");
                return;
            }
            StringBuilder sb = new StringBuilder(1024);
            IniFile.GetPrivateProfileString("Login", "password", "null", sb, 1024, fileInfo.FullName);
             User.Password = Decrypt(sb.ToString(), "1a2b3d4e");
            //User.Password = sb.ToString();
            sb.Clear();
        }
        /// <summary>
        /// 保存密码到ini文件
        /// </summary>
        public static void SaveIsRemember()
        {
            IniFile.WritePrivateProfileString("Login", "password", User.Password, Directory.GetCurrentDirectory() + "/configLogin.ini");
        }

        private void btnLogOff_Click(object sender, EventArgs e)
        {
            User.IsLogin = false;
            FormMain.AdminRight(User.IsLogin);
            this.Close();
        }
        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>  
        /// C# DES加密方法  
        /// </summary>  
        /// <param name="encryptedValue">要加密的字符串</param>  
        /// <param name="key">密钥</param>  
        /// <param name="iv">向量</param>  
        /// <returns>加密后的字符串</returns>

        public static string Encrypt(string encryptString, string encryptKey)
        {
            try
            {
                byte[] Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] IV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="input">待解密的字符串</param>
        /// <param name="key">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>

        public static string Decrypt(string descryptString, string descryptKey)
        {
            try
            {
                byte[] Key = Encoding.UTF8.GetBytes(descryptKey.Substring(0, 8));
                byte[] IV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(descryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return descryptString;
            }

        }
        private void FormLogin_Load(object sender, EventArgs e)
        {
            txtPassword.Text = "";
        }
    }
}
